#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class Web_UserMessageProvider : CY.GFive.Core.Providers.DALProvider.IWeb_UserMessageProvider
	{
		#region Sql
		private static readonly string SqlSelectWeb_UserMessage = "SELECT [Id], [User_ID],[Message_ID],[UserMessage_State] FROM [Web_UserMessage] WHERE  [Id]=@Id";

		private static readonly string SqlInsertWeb_UserMessage = "INSERT INTO [Web_UserMessage]([User_ID],[Message_ID],[UserMessage_State]) values(@User_ID,@Message_ID,@UserMessage_State);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateWeb_UserMessage = "UPDATE [Web_UserMessage] SET [User_ID] = @User_ID,[Message_ID] = @Message_ID,[UserMessage_State] = @UserMessage_State WHERE  [Id]=@Id";

		private static readonly string SqlDeleteWeb_UserMessage = "DELETE [Web_UserMessage] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllWeb_UserMessage = "SELECT [Id], [User_ID],[Message_ID],[UserMessage_State] FROM [Web_UserMessage]";
		#endregion

		#region IWeb_UserMessageProvider Members
		public Core.Business.Web_UserMessage Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectWeb_UserMessage);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Web_UserMessage web_UserMessage = new Core.Business.Web_UserMessage();

				if (!reader.IsDBNull(0)) web_UserMessage.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) web_UserMessage.User_ID = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) web_UserMessage.Message_ID = reader.GetInt32(2);
				if (!reader.IsDBNull(3)) web_UserMessage.UserMessage_State = reader.GetInt32(3);

				return web_UserMessage;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Web_UserMessage web_UserMessage)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@User_ID", SqlDbType.Int, web_UserMessage.User_ID);
			sql.AddParameter("@Message_ID", SqlDbType.Int, web_UserMessage.Message_ID);
			sql.AddParameter("@UserMessage_State", SqlDbType.Int, web_UserMessage.UserMessage_State);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertWeb_UserMessage);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) web_UserMessage.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Web_UserMessage web_UserMessage)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@User_ID", SqlDbType.Int, web_UserMessage.User_ID);
			sql.AddParameter("@Message_ID", SqlDbType.Int, web_UserMessage.Message_ID);
			sql.AddParameter("@UserMessage_State", SqlDbType.Int, web_UserMessage.UserMessage_State);
			sql.AddParameter("@Id", SqlDbType.Int, web_UserMessage.Id);
			sql.ExecuteSql(SqlUpdateWeb_UserMessage);
		}

		public void Delete(Core.Business.Web_UserMessage web_UserMessage)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, web_UserMessage.Id);
			sql.ExecuteSql(SqlDeleteWeb_UserMessage);
		}

		public IList<Core.Business.Web_UserMessage> GetAllWeb_UserMessage()
		{
			IList<Core.Business.Web_UserMessage> web_UserMessagelist = new List<Core.Business.Web_UserMessage>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllWeb_UserMessage);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Web_UserMessage web_UserMessage = new Core.Business.Web_UserMessage();

					if (!reader.IsDBNull(0)) web_UserMessage.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) web_UserMessage.User_ID = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) web_UserMessage.Message_ID = reader.GetInt32(2);
					if (!reader.IsDBNull(3)) web_UserMessage.UserMessage_State = reader.GetInt32(3);

					web_UserMessage.MarkOld();
					web_UserMessagelist.Add(web_UserMessage);
				}
				reader.Close();
			}
			return web_UserMessagelist;
		}
		#endregion
	}
}
